package com.example.administrator.mobile;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class ElsFragment extends Fragment {
    ArrayList<Els>elsArrayList;
    ListView mLvEls;


    public ElsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_els, container, false);
        mLvEls =view.findViewById(R.id.lv_els);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
    }


    private void initView() {
        MyBaseAdapter adapter = new MyBaseAdapter();
        mLvEls.setAdapter(adapter);

    }

    private void initData() {
        String str = readsjJsonFile();
        elsArrayList = parsesjJSON(str);

    }

    private ArrayList<Els> parsesjJSON(String str) {
        ArrayList<Els>elsArrayList= new ArrayList<>();

        try {
            JSONArray jsonArray = new JSONArray(str);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                Els els = new Els();
                els.setIcon(jsonObject.optString("icon"));
                els.setTitle(jsonObject.optString("title"));

                elsArrayList.add(els);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return elsArrayList;
    }

    private String readsjJsonFile() {
        String str = "";
        InputStream inputStream = getResources().openRawResource(R.raw.az);

        try {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            str = new String(bytes);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return str;
        }
    }

    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return elsArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return elsArrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            View view = View.inflate(getActivity(), R.layout.list_layout, null);
            ImageView icon = view.findViewById(R.id.iv_icon);

            String pic = elsArrayList.get(position).getIcon();
            int mipmap = getResources().getIdentifier(pic.substring(0, pic.indexOf(".")), "mipmap", getActivity().getPackageName());
            icon.setImageResource(mipmap);
            TextView tvTiltle = view.findViewById(R.id.tv_title);
            tvTiltle.setText(elsArrayList.get(position).getTitle());

            return view;
        }

    }
    class Els{

        private String icon;
        private String title;


        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

    }

}
